package com.cooper.scraper;

import java.io.File;

import org.apache.http.HttpVersion;
import org.apache.http.client.HttpClient;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.params.ConnManagerParams;
import org.apache.http.conn.params.ConnPerRoute;
import org.apache.http.conn.routing.HttpRoute;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.protocol.HTTP;

import android.os.Environment;

public class DownloadParams {

	
	//private Handler handler;
	private String bitmapCacheDir = "/.cooper_soft";

	private int retryCount = 2;
	private int connManagerTimeout = 2000;
	private int connTimeout = 30000;
	private int socketTimeout = 60000;
	private int connPoolMax = 20;

	private int pageBufferSize = 20;
	private int bitmapBufferSize = 900;

	// private int threadPoolSize = 10;
	// private int threadMaxSize = 200;

	private boolean onlyWIFI = false;

	private boolean saveBitmapBufferToFile = false;

	public DownloadParams() {
		super();
	}

	public DownloadParams(int retryCount, int connTimeout, int socketTimeout,
			int connManagerTimeout) {
		super();
		this.retryCount = retryCount;
		this.connTimeout = connTimeout;
		this.socketTimeout = socketTimeout;
		this.connManagerTimeout = connManagerTimeout;
	}

	public String getBitmapCacheDir() {
		String result = Environment.getExternalStorageDirectory().toString()
				+ ((bitmapCacheDir.trim().startsWith("/") || bitmapCacheDir
						.trim().startsWith("\\")) ? bitmapCacheDir.trim() : "/"
						+ bitmapCacheDir.trim());
		File defFolder = new File(result);
		if (!defFolder.exists()) {
			defFolder.mkdirs();
		}
		return result;
	}

	public void setBitmapCacheDir(String bitmapCacheDir) {
		this.bitmapCacheDir = bitmapCacheDir;
	}

	public int getRetryCount() {
		return retryCount;
	}

	public void setRetryCount(int retryCount) {
		this.retryCount = retryCount;
	}

	public int getConnManagerTimeout() {
		return connManagerTimeout;
	}

	public int getConnTimeout() {
		return connTimeout;
	}

	public int getSocketTimeout() {
		return socketTimeout;
	}
	
	public int getConnPoolMax() {
		return connPoolMax;
	}

	public void setConnPoolMax(int connPoolMax) {
		this.connPoolMax = connPoolMax;
	}

	public int getPageBufferSize() {
		return pageBufferSize;
	}

	public void setPageBufferSize(int pageBufferSize) {
		this.pageBufferSize = pageBufferSize;
	}

	public int getBitmapBufferSize() {
		return bitmapBufferSize;
	}

	public void setBitmapBufferSize(int bitmapBufferSize) {
		this.bitmapBufferSize = bitmapBufferSize;
	}

	public boolean isSaveBitmapBufferToFile() {
		return saveBitmapBufferToFile;
	}

	public void setSaveBitmapBufferToFile(boolean saveBitmapBufferToFile) {
		this.saveBitmapBufferToFile = saveBitmapBufferToFile;
	}

	public boolean isOnlyWIFI() {
		return onlyWIFI;
	}

	public void setOnlyWIFI(boolean onlyWIFI) {
		this.onlyWIFI = onlyWIFI;
	}

	private HttpClient customHttpClient;

	public synchronized HttpClient getHttpClient() {
		if (customHttpClient == null) {
			BasicHttpParams httpParameters = new BasicHttpParams();

			HttpProtocolParams.setVersion(httpParameters, HttpVersion.HTTP_1_1);
			HttpProtocolParams.setContentCharset(httpParameters,
					HTTP.DEFAULT_CONTENT_CHARSET);
			HttpProtocolParams.setUseExpectContinue(httpParameters, true);
			HttpProtocolParams
					.setUserAgent(
							httpParameters,
							"Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.2; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)");
			ConnManagerParams.setTimeout(httpParameters, connManagerTimeout);
			
			ConnManagerParams.setMaxConnectionsPerRoute(httpParameters, new ConnPerRoute(){

				@Override
				public int getMaxForRoute(HttpRoute route) {
	
					return connPoolMax;
				}
				
			});

			// ----------only this setting wrok is ok
			HttpConnectionParams.setConnectionTimeout(httpParameters, connTimeout);
			HttpConnectionParams.setSoTimeout(httpParameters, socketTimeout);
			// --------------------------------------

			SchemeRegistry schReg = new SchemeRegistry();
			schReg.register(new Scheme("http", PlainSocketFactory
					.getSocketFactory(), 80));
			schReg.register(new Scheme("https",
					SSLSocketFactory.getSocketFactory(), 443));
						
			ClientConnectionManager conMgr = new ThreadSafeClientConnManager(
					httpParameters, schReg);
			
			customHttpClient = new DefaultHttpClient(conMgr,httpParameters);
			
		}
		return customHttpClient;

	}

}
